<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="WTTWi2XnJ5">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Josefin Sans:300,300italic,400,400italic,700,700italic|Lobster:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"lclong.top","root":"/","scheme":"Gemini","version":"7.7.1","exturl":true,"sidebar":{"position":"left","display":"post","padding":20,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="简介本科上java课时做的俄罗斯方块，计算机网络课又把俄罗斯方块改造成了联机对打。房主创建房间，对手通过输入房主的ip来加入对战。其中俄罗斯方块主体部分用到了NetBeans的图形化开发工具，使用idea要装插件。 github地址">
<meta property="og:type" content="article">
<meta property="og:title" content="Java实现俄罗斯联机俄罗斯方块">
<meta property="og:url" content="https://lclong.top/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/index.html">
<meta property="og:site_name" content="Zlatanlong&#39;s T-blog">
<meta property="og:description" content="简介本科上java课时做的俄罗斯方块，计算机网络课又把俄罗斯方块改造成了联机对打。房主创建房间，对手通过输入房主的ip来加入对战。其中俄罗斯方块主体部分用到了NetBeans的图形化开发工具，使用idea要装插件。 github地址">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2018-11-17T13:24:34.000Z">
<meta property="article:modified_time" content="2020-03-19T04:22:04.000Z">
<meta property="article:author" content="Zlatanlong">
<meta property="article:tag" content="socket">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://lclong.top/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>Java实现俄罗斯联机俄罗斯方块 | Zlatanlong's T-blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Zlatanlong's T-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">Lakers is Championship!</h1>
      
  </div>

  <div class="site-nav-right"></div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">31</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">38</span></a>

  </li>
        <li class="menu-item menu-item-aboutme">

    <a href="/aboutme/" rel="section"><i class="fa fa-fw fa-user"></i>博主</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://lclong.top/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Zlatanlong">
      <meta itemprop="description" content="生活就像巧克力🍫">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zlatanlong's T-blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          Java实现俄罗斯联机俄罗斯方块
        </h2>

        <div class="post-meta">
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-11-17 21:24:34" itemprop="dateCreated datePublished" datetime="2018-11-17T21:24:34+08:00">2018-11-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-19 12:22:04" itemprop="dateModified" datetime="2020-03-19T12:22:04+08:00">2020-03-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>
            </span>

          
            <span id="/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/" class="post-meta-item leancloud_visitors" data-flag-title="Java实现俄罗斯联机俄罗斯方块" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">评论次数：</span>
    
    <a title="valine" href="/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>40k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1:39</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>本科上java课时做的俄罗斯方块，计算机网络课又把俄罗斯方块改造成了联机对打。房主创建房间，对手通过输入房主的ip来加入对战。其中俄罗斯方块主体部分用到了NetBeans的图形化开发工具，使用idea要装插件。</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL1psYXRhbmxvbmcvSkFWQS10ZXRyaXM=" title="https://github.com/Zlatanlong/JAVA-tetris">github地址<i class="fa fa-external-link"></i></span></p>
<a id="more"></a>

<h1 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$***/src tree</span><br><span class="line">.</span><br><span class="line">|____pictures</span><br><span class="line">| |____巴西.png</span><br><span class="line">|____mytetris</span><br><span class="line">| |____CountPanel.java</span><br><span class="line">| |____.DS_Store</span><br><span class="line">| |____CountPanel.form</span><br><span class="line">| |____MainFrame.java</span><br><span class="line">| |____Controller.java</span><br><span class="line">| |____GamePanel.java</span><br><span class="line">| |____GameState.java</span><br><span class="line">| |____GamePanel.form</span><br><span class="line">| |____Block.java</span><br><span class="line">| |____MainFrame.form</span><br><span class="line">|____socket</span><br><span class="line">| |____GameServer.java</span><br><span class="line">| |____SocketUtil.java</span><br><span class="line">| |____GameClient.java</span><br></pre></td></tr></table></figure>

<p>其中pictures是俄罗斯方块的一个图片素材，java课时值2018年俄罗斯世界杯，我是🇧🇷球迷，就用了巴西国旗做按钮背景。</p>
<p><code>mytetris文件夹</code>是俄罗斯方块的游戏主体部分，</p>
<p><code>socket文件夹</code>下是俄罗斯方块的联机部分，通过<code>socket</code>实现。</p>
<h1 id="俄罗斯方块主体代码"><a href="#俄罗斯方块主体代码" class="headerlink" title="俄罗斯方块主体代码"></a>俄罗斯方块主体代码</h1><h2 id="Controller-java"><a href="#Controller-java" class="headerlink" title="Controller.java"></a>Controller.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mytetris;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">import</span> socket.SocketUtil;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> removeLog = <span class="number">0</span>;<span class="comment">//同时消行数量</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化状态</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">int</span> currentX = <span class="number">3</span>;</span><br><span class="line">    <span class="keyword">int</span> currentY = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[][] fix = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">10</span>][<span class="number">20</span>];<span class="comment">// 把整个界面分割成10*20</span></span><br><span class="line">    <span class="keyword">private</span> GameState state = <span class="keyword">new</span> GameState();</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 产生一个新的形状</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getNewShape</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        getState().getNewBlock();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Block <span class="title">getCurrentShape</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getState().getCurrentBlock();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Block <span class="title">getNextShape</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getState().getNextBlock();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化界面数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inintFix</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt;= <span class="number">9</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt;= <span class="number">19</span>; k++) &#123;</span><br><span class="line">                fix[j][k] = <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断是否出界</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> x X轴偏移量</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> y Y轴偏移量</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isValid</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] tempShape = getCurrentShape().getCurrentBlocks();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i += <span class="number">2</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> ((tempShape[i + <span class="number">1</span>] + y) &lt; <span class="number">0</span> || (tempShape[i + <span class="number">1</span>] + y) &gt; <span class="number">19</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ((tempShape[i] + x) &lt; <span class="number">0</span> || (tempShape[i] + x) &gt; <span class="number">9</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (fix[tempShape[i] + x][tempShape[i + <span class="number">1</span>] + y] != <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 上下左右和翻转 先判断是否出界</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">left</span><span class="params">(Boolean ifSend)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (isValid(currentX - <span class="number">1</span>, currentY)) &#123;</span><br><span class="line">            currentX--;</span><br><span class="line">            <span class="keyword">if</span> (ifSend) &#123;</span><br><span class="line">                SocketUtil.send(MainFrame.socket, <span class="string">"move37"</span>);</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">right</span><span class="params">(Boolean ifSend)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (isValid(currentX + <span class="number">1</span>, currentY)) &#123;</span><br><span class="line">            currentX++;</span><br><span class="line">            <span class="keyword">if</span> (ifSend) &#123;</span><br><span class="line">                SocketUtil.send(MainFrame.socket, <span class="string">"move39"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 方块下落，落不下去的就死掉了</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">down</span><span class="params">(Boolean ifSend)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (ifSend) &#123;</span><br><span class="line">            SocketUtil.send(MainFrame.socket, <span class="string">"move40"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (isValid(currentX, currentY + <span class="number">1</span>)) &#123;</span><br><span class="line">            currentY++;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            add(currentX, currentY, ifSend);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">turn</span><span class="params">(Boolean ifSend)</span> </span>&#123;</span><br><span class="line">        getState().getCurrentBlock().next();</span><br><span class="line">        <span class="keyword">if</span> (!isValid(currentX, currentY)) &#123;</span><br><span class="line">            getState().getCurrentBlock().forward();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (ifSend) &#123;</span><br><span class="line">                SocketUtil.send(MainFrame.socket, <span class="string">"move38"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 把死掉的方块变成墙；</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> x</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> y</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y, Boolean ifMy)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] tempShape = getState().getCurrentBlock().getCurrentBlocks();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i += <span class="number">2</span>) &#123;</span><br><span class="line">            fix[x + tempShape[i]][y + tempShape[i + <span class="number">1</span>]] = getState().getCurrentBlock().getIColor() + <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        remove();</span><br><span class="line">        currentX = <span class="number">3</span>;</span><br><span class="line">        currentY = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (ifMy) &#123;</span><br><span class="line">            getNewShape();</span><br><span class="line">            MainFrame.changeNext();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 消除可消的一行</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">19</span>; i &gt; <span class="number">0</span>; i--) &#123;</span><br><span class="line">            <span class="comment">//i是一共20行</span></span><br><span class="line">            <span class="keyword">int</span> flag = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (fix[j][i] == <span class="number">0</span>) &#123;</span><br><span class="line">                    flag = <span class="number">1</span>;<span class="comment">//一行已经满了</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (flag == <span class="number">0</span>) &#123;</span><br><span class="line">                state.setCount(state.getCount() + state.getPoint() + removeLog);</span><br><span class="line">                state.changeInterval();</span><br><span class="line">                MainFrame.changeCount();</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">                    fix[j][i] = <span class="number">0</span>;</span><br><span class="line">                &#125;<span class="comment">//消除这一行</span></span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = i; k &gt; <span class="number">0</span>; k--) &#123;</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">                        fix[j][k] = fix[j][k - <span class="number">1</span>];</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;<span class="comment">//其他行下移一行</span></span><br><span class="line">                removeLog++;</span><br><span class="line">                remove();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//判断第一行是否要被消除</span></span><br><span class="line">        <span class="keyword">int</span> flag0 = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (fix[j][<span class="number">0</span>] == <span class="number">0</span>) &#123;</span><br><span class="line">                flag0 = <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag0 == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">                fix[j][<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        removeLog = <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 道具1</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">prop1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">            fix[j][<span class="number">19</span>] = <span class="number">0</span>;</span><br><span class="line">        &#125;<span class="comment">//消除这一行</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">19</span>; k &gt; <span class="number">0</span>; k--) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">                fix[j][k] = fix[j][k - <span class="number">1</span>];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="comment">//其他行下移一行</span></span><br><span class="line">        state.setCount(state.getCount() + <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">        state.changeInterval();</span><br><span class="line">        MainFrame.changeCount();</span><br><span class="line">        MainFrame.gp.repaint();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the state</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> GameState <span class="title">getState</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> state;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Block-java"><a href="#Block-java" class="headerlink" title="Block.java"></a>Block.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mytetris;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.awt.Color;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> socket.SocketUtil;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Zlatan</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Block</span> </span>&#123;</span><br><span class="line">    List&lt;<span class="keyword">int</span>[][]&gt; allBlocks = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    List&lt;Color&gt; colors = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    <span class="keyword">int</span> i;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Block</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        allBlocks.add(tblocks);</span><br><span class="line">        colors.add(Color.yellow);</span><br><span class="line">        allBlocks.add(lblocks);</span><br><span class="line">        colors.add(Color.blue);</span><br><span class="line">        allBlocks.add(iblocks);</span><br><span class="line">        colors.add(Color.green);</span><br><span class="line">        allBlocks.add(flblocks);</span><br><span class="line">        colors.add(Color.cyan);</span><br><span class="line">        allBlocks.add(zblocks);</span><br><span class="line">        colors.add(Color.pink);</span><br><span class="line">        allBlocks.add(fzblocks);</span><br><span class="line">        colors.add(Color.white);</span><br><span class="line">        allBlocks.add(oblocks);</span><br><span class="line">        colors.add(Color.orange);</span><br><span class="line">        i=(<span class="keyword">int</span>)(<span class="number">0</span>+Math.random()*(<span class="number">6</span>-<span class="number">0</span>+<span class="number">1</span>));<span class="comment">//(数据类型)(最小值+Math.random()*(最大值-最小值+1))</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Block</span><span class="params">(<span class="keyword">int</span> i)</span></span>&#123;</span><br><span class="line">        allBlocks.add(tblocks);</span><br><span class="line">        colors.add(Color.yellow);</span><br><span class="line">        allBlocks.add(lblocks);</span><br><span class="line">        colors.add(Color.blue);</span><br><span class="line">        allBlocks.add(iblocks);</span><br><span class="line">        colors.add(Color.green);</span><br><span class="line">        allBlocks.add(flblocks);</span><br><span class="line">        colors.add(Color.cyan);</span><br><span class="line">        allBlocks.add(zblocks);</span><br><span class="line">        colors.add(Color.pink);</span><br><span class="line">        allBlocks.add(fzblocks);</span><br><span class="line">        colors.add(Color.white);</span><br><span class="line">        allBlocks.add(oblocks);</span><br><span class="line">        colors.add(Color.orange);</span><br><span class="line">        <span class="keyword">this</span>.i = i;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> state=<span class="number">0</span>;<span class="comment">//当前的数</span></span><br><span class="line">    <span class="comment">//格式:(第一块，X轴，Y轴；第二块......)</span></span><br><span class="line">    <span class="comment">//shape of T</span></span><br><span class="line">    <span class="keyword">int</span>[][] tblocks=&#123;</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>&#125;            </span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//shape of L</span></span><br><span class="line">    <span class="keyword">int</span>[][] lblocks=&#123;</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">2</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;            </span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//shape of I</span></span><br><span class="line">    <span class="keyword">int</span>[][]iblocks=&#123;</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="number">0</span>&#125;,</span><br><span class="line">        &#123;<span class="number">2</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="number">0</span>&#125;,</span><br><span class="line">        &#123;<span class="number">2</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">int</span>[][]flblocks=&#123;</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>&#125;   </span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">int</span>[][]zblocks=&#123;</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>&#125;  </span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">int</span>[][]fzblocks=&#123;</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>&#125;,</span><br><span class="line">        &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>&#125; </span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">int</span>[][]oblocks=&#123;</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;,</span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>&#125;  </span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">next</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        state = state == <span class="number">3</span> ? <span class="number">0</span> : state + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">forward</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        state = state == <span class="number">0</span> ? <span class="number">3</span> : state - <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回当前形状数组</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getCurrentBlocks() &#123;</span><br><span class="line">            <span class="keyword">return</span> allBlocks.get(i)[state];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> Color <span class="title">getColor</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> colors.get(i);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getIColor</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (MainFrame.mod==<span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">8</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getI</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="GameState-java"><a href="#GameState-java" class="headerlink" title="GameState.java"></a>GameState.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mytetris;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> socket.SocketUtil;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GameState</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Block currentBlock; <span class="comment">// 当前形状</span></span><br><span class="line">    <span class="keyword">private</span> Block nextBlock; <span class="comment">// 下一个形状</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 总得分</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> point; <span class="comment">// 每次加分</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> interval; <span class="comment">// 时间间隔，影响速度</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">GameState</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.nextBlock = <span class="keyword">new</span> Block();</span><br><span class="line">        SocketUtil.send(MainFrame.socket, <span class="string">"fBlock"</span> + Integer.toString(nextBlock.getI()));</span><br><span class="line">        <span class="keyword">this</span>.interval = <span class="number">1000</span>;</span><br><span class="line">        <span class="keyword">this</span>.count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">this</span>.point = <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 产生一个新的形状</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getNewBlock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        currentBlock = nextBlock;</span><br><span class="line">        nextBlock = <span class="keyword">new</span> Block();</span><br><span class="line">        SocketUtil.send(MainFrame.socket, <span class="string">"nBlock"</span> + Integer.toString(nextBlock.getI()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNewBlock</span><span class="params">(Block block)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.currentBlock = <span class="keyword">this</span>.nextBlock;</span><br><span class="line">        <span class="keyword">this</span>.nextBlock = block;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">changeInterval</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (MainFrame.mod != <span class="number">2</span> &amp;&amp; (<span class="number">1000</span> - count * <span class="number">9</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            interval = <span class="number">1000</span> - count * <span class="number">9</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the currentBlock</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Block <span class="title">getCurrentBlock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> currentBlock;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> currentBlock the currentBlock to set</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCurrentBlock</span><span class="params">(Block currentBlock)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.currentBlock = currentBlock;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the nextBlock</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Block <span class="title">getNextBlock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> nextBlock;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> nextBlock the nextBlock to set</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNextBlock</span><span class="params">(Block nextBlock)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.nextBlock = nextBlock;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the count</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> count the count to set</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCount</span><span class="params">(<span class="keyword">int</span> count)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.count = count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the point</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getPoint</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> point;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> point the point to set</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPoint</span><span class="params">(<span class="keyword">int</span> point)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.point = point;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the interval</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getInterval</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> interval;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> interval the interval to set</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setInterval</span><span class="params">(<span class="keyword">int</span> interval)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.interval = interval;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="CountPanel-java"><a href="#CountPanel-java" class="headerlink" title="CountPanel.java"></a>CountPanel.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mytetris;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.awt.Color;</span><br><span class="line"><span class="keyword">import</span> java.awt.Graphics;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Level;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Logger;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> dmt</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CountPanel</span> <span class="keyword">extends</span> <span class="title">javax</span>.<span class="title">swing</span>.<span class="title">JPanel</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> prop1Count = <span class="number">5</span>;</span><br><span class="line">    <span class="keyword">private</span> Block nextBlock;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CountPanel</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        initComponents();</span><br><span class="line">        prop1show.setText(<span class="string">"数量："</span>+prop1Count);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 显示结束</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showOver</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        show.setText(<span class="string">"GAMEOVER!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 删除Over标致</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delOver</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        show.setText(<span class="string">""</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Thread.currentThread().sleep(<span class="number">1000</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException ex) &#123;</span><br><span class="line">                Logger.getLogger(GamePanel<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawBlocks</span><span class="params">(Graphics g)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] shape = nextBlock.getCurrentBlocks();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; shape.length; i += <span class="number">2</span>) &#123;</span><br><span class="line">            g.setColor(Color.black);</span><br><span class="line">            g.drawRect(<span class="number">20</span> * (shape[i])+<span class="number">15</span> , <span class="number">20</span> * (shape[i + <span class="number">1</span>])+<span class="number">250</span> , <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">            g.setColor(nextBlock.getColor());</span><br><span class="line">            g.fillRect(<span class="number">20</span> * (shape[i])+<span class="number">15</span> , <span class="number">20</span> * (shape[i + <span class="number">1</span>])+<span class="number">250</span> , <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">paintComponent</span><span class="params">(Graphics g)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.paintComponent(g);</span><br><span class="line">        drawBlocks(g);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * This method is called from within the constructor to initialize the form.</span></span><br><span class="line"><span class="comment">     * WARNING: Do NOT modify this code. The content of this method is always</span></span><br><span class="line"><span class="comment">     * regenerated by the Form Editor.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line">    <span class="comment">// &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;//GEN-BEGIN:initComponents</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initComponents</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        currrentcount = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line">        count = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line">        jLabel1 = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line">        show = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line">        prop1 = <span class="keyword">new</span> javax.swing.JButton();</span><br><span class="line">        prop1show = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line"></span><br><span class="line">        currrentcount.setText(<span class="string">"当前得分:"</span>);</span><br><span class="line"></span><br><span class="line">        count.setText(<span class="string">"0"</span>);</span><br><span class="line"></span><br><span class="line">        jLabel1.setText(<span class="string">"下一图案："</span>);</span><br><span class="line"></span><br><span class="line">        show.setFont(<span class="keyword">new</span> java.awt.Font(<span class="string">"Lucida Grande"</span>, <span class="number">1</span>, <span class="number">13</span>)); <span class="comment">// NOI18N</span></span><br><span class="line">        show.setForeground(<span class="keyword">new</span> java.awt.Color(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line"></span><br><span class="line">        prop1.setText(<span class="string">"道具"</span>);</span><br><span class="line">        prop1.setFocusable(<span class="keyword">false</span>);</span><br><span class="line">        prop1.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                prop1ActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        prop1show.setText(<span class="string">"数量：0"</span>);</span><br><span class="line"></span><br><span class="line">        javax.swing.GroupLayout layout = <span class="keyword">new</span> javax.swing.GroupLayout(<span class="keyword">this</span>);</span><br><span class="line">        <span class="keyword">this</span>.setLayout(layout);</span><br><span class="line">        layout.setHorizontalGroup(</span><br><span class="line">            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">            .addGroup(layout.createSequentialGroup()</span><br><span class="line">                .addContainerGap()</span><br><span class="line">                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()</span><br><span class="line">                        .addGap(<span class="number">0</span>, <span class="number">0</span>, Short.MAX_VALUE)</span><br><span class="line">                        .addComponent(count, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">62</span>, javax.swing.GroupLayout.PREFERRED_SIZE))</span><br><span class="line">                    .addComponent(show, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)</span><br><span class="line">                    .addGroup(layout.createSequentialGroup()</span><br><span class="line">                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">                            .addComponent(prop1show, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">72</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                            .addComponent(jLabel1)</span><br><span class="line">                            .addComponent(currrentcount, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">90</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                            .addComponent(prop1))</span><br><span class="line">                        .addGap(<span class="number">0</span>, <span class="number">4</span>, Short.MAX_VALUE)))</span><br><span class="line">                .addContainerGap())</span><br><span class="line">        );</span><br><span class="line">        layout.setVerticalGroup(</span><br><span class="line">            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">            .addGroup(layout.createSequentialGroup()</span><br><span class="line">                .addGap(<span class="number">31</span>, <span class="number">31</span>, <span class="number">31</span>)</span><br><span class="line">                .addComponent(currrentcount, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">33</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addGap(<span class="number">18</span>, <span class="number">18</span>, <span class="number">18</span>)</span><br><span class="line">                .addComponent(count, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">52</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)</span><br><span class="line">                .addComponent(show, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">43</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)</span><br><span class="line">                .addComponent(jLabel1)</span><br><span class="line">                .addGap(<span class="number">137</span>, <span class="number">137</span>, <span class="number">137</span>)</span><br><span class="line">                .addComponent(prop1)</span><br><span class="line">                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)</span><br><span class="line">                .addComponent(prop1show, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">28</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addContainerGap(<span class="number">199</span>, Short.MAX_VALUE))</span><br><span class="line">        );</span><br><span class="line">    &#125;<span class="comment">// &lt;/editor-fold&gt;//GEN-END:initComponents</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">prop1ActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_prop1ActionPerformed</span></span><br><span class="line">        <span class="comment">// 使用道具1:</span></span><br><span class="line">        <span class="keyword">if</span> (prop1Count&gt;<span class="number">0</span>&amp;&amp;!MainFrame.isOver) &#123;</span><br><span class="line"><span class="comment">//            Controller.prop1();</span></span><br><span class="line">            prop1Count--;</span><br><span class="line">            prop1show.setText(<span class="string">"数量："</span>+prop1Count);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_prop1ActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// Variables declaration - do not modify//GEN-BEGIN:variables</span></span><br><span class="line">    <span class="keyword">public</span> javax.swing.JLabel count;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JLabel currrentcount;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JLabel jLabel1;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JButton prop1;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JLabel prop1show;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JLabel show;</span><br><span class="line">    <span class="comment">// End of variables declaration//GEN-END:variables</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setProp1Count</span><span class="params">(<span class="keyword">int</span> prop1Count)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.prop1Count = prop1Count;</span><br><span class="line">        <span class="keyword">this</span>.prop1show.setText(<span class="string">"数量："</span>+prop1Count);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCountText</span><span class="params">(<span class="keyword">int</span> count)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.count.setText(Integer.toString(count));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> nextBlock the nextBlock to set</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNextBlock</span><span class="params">(Block nextBlock)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.nextBlock = nextBlock;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="GamePanel-java"><a href="#GamePanel-java" class="headerlink" title="GamePanel.java"></a>GamePanel.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mytetris;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.awt.Color;</span><br><span class="line"><span class="keyword">import</span> java.awt.Graphics;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Level;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Logger;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> dmt 整个gamepanel分为10*20（宽*高） 200 块 每一块根据fix[x][y]的值来区分颜色，其中若为0</span></span><br><span class="line"><span class="comment"> * 不单独添加颜色，相当于正常 10为红色，现在导致游戏over的那个形状会被画成红色，其他颜色均代表墙。 1-7代表7中颜色，9为不区分形状颜色时墙的颜色</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GamePanel</span> <span class="keyword">extends</span> <span class="title">javax</span>.<span class="title">swing</span>.<span class="title">JPanel</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    Controller controller = <span class="keyword">new</span> Controller();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">GamePanel</span><span class="params">(Boolean ifMy)</span> </span>&#123;</span><br><span class="line">        initComponents();</span><br><span class="line">        controller.getNewShape();</span><br><span class="line">        controller.inintFix();</span><br><span class="line">        <span class="keyword">if</span> (ifMy) &#123;</span><br><span class="line">            <span class="keyword">new</span> Thread(<span class="keyword">this</span>).start();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (!MainFrame.isOver) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (!MainFrame.isPause) &#123;</span><br><span class="line">                    controller.down(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">this</span>.repaint();</span><br><span class="line">                &#125;</span><br><span class="line">                Thread.currentThread().sleep(controller.getState().getInterval());</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException ex) &#123;</span><br><span class="line">                Logger.getLogger(GamePanel<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">paintComponent</span><span class="params">(Graphics g)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.paintComponent(g);</span><br><span class="line">        drawBlocks(g, controller.currentX, controller.currentY);</span><br><span class="line">        drawFix(g);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 画墙</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> g</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawFix</span><span class="params">(Graphics g)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt;= <span class="number">9</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt;= <span class="number">19</span>; k++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">1</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.yellow);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">2</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.blue);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">3</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.green);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">4</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.cyan);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">5</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.pink);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">6</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.white);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">7</span>) &#123;</span><br><span class="line">                    g.setColor(Color.black);</span><br><span class="line">                    g.drawRect(<span class="number">20</span> * j, <span class="number">20</span> * k, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                    g.setColor(Color.orange);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">9</span>) &#123;</span><br><span class="line">                    g.setColor(Color.lightGray);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 红色警告</span></span><br><span class="line">                <span class="keyword">if</span> (controller.fix[j][k] == <span class="number">10</span>) &#123;</span><br><span class="line">                    g.setColor(Color.red);</span><br><span class="line">                    g.fillRect(j * <span class="number">20</span>, k * <span class="number">20</span>, <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 画形状 同时判断结束, 判断逻辑： 如果要画形状的时候当前位置已经有墙了，说明墙已经到了最上面， 因此游戏结束</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> g</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> x</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> y</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawBlocks</span><span class="params">(Graphics g, <span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] shape = controller.getCurrentShape().getCurrentBlocks();</span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; shape.length; i++) &#123;</span></span><br><span class="line"><span class="comment">//            int j = shape[i];</span></span><br><span class="line"><span class="comment">//            System.out.print(j);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//        System.out.println("huanhang");</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; shape.length; i += <span class="number">2</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (controller.fix[shape[i] + x][shape[i + <span class="number">1</span>] + y] != <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="comment">// 把结束的这个形状位置号标为10， 会化成红色</span></span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; shape.length; j += <span class="number">2</span>) &#123;</span><br><span class="line">                    controller.fix[shape[j] + x][shape[j + <span class="number">1</span>] + y] = <span class="number">10</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                MainFrame.over();</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; shape.length; i += <span class="number">2</span>) &#123;</span><br><span class="line">            g.setColor(Color.black);</span><br><span class="line">            g.drawRect(<span class="number">20</span> * (shape[i] + x), <span class="number">20</span> * (shape[i + <span class="number">1</span>] + y), <span class="number">19</span>, <span class="number">19</span>);</span><br><span class="line">            g.setColor(controller.getCurrentShape().getColor());</span><br><span class="line">            g.fillRect(<span class="number">20</span> * (shape[i] + x), <span class="number">20</span> * (shape[i + <span class="number">1</span>] + y), <span class="number">18</span>, <span class="number">18</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Block <span class="title">getNextBlock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> controller.getNextShape();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 展示时设置第一个Block</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> block</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setFirstBlock</span><span class="params">(Block block)</span> </span>&#123;</span><br><span class="line">        getGameState().setNextBlock(block);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNewBlock</span><span class="params">(Block block)</span> </span>&#123;</span><br><span class="line">        getGameState().setNewBlock(block);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> GameState <span class="title">getGameState</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> controller.getState();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    public void changeCount() &#123;</span></span><br><span class="line"><span class="comment">//        controller.getState().changeCount();</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">keyPressed</span><span class="params">(java.awt.event.KeyEvent evt)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ((!MainFrame.isOver) &amp;&amp; (!MainFrame.isPause)) &#123;</span><br><span class="line">            <span class="keyword">switch</span> (evt.getKeyCode()) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">37</span>:</span><br><span class="line">                    controller.left(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">39</span>:</span><br><span class="line">                    controller.right(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">40</span>:</span><br><span class="line">                    controller.down(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">38</span>:</span><br><span class="line">                    controller.turn(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">65</span>:</span><br><span class="line">                    controller.left(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">68</span>:</span><br><span class="line">                    controller.right(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">83</span>:</span><br><span class="line">                    controller.down(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">87</span>:</span><br><span class="line">                    controller.turn(<span class="keyword">true</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">default</span>:</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            repaint();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getPressed</span><span class="params">(String code)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ((!MainFrame.isOver) &amp;&amp; (!MainFrame.isPause)) &#123;</span><br><span class="line">            <span class="keyword">if</span> (code.equals(<span class="string">"move37"</span>)) &#123;</span><br><span class="line">                controller.left(<span class="keyword">false</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code.equals(<span class="string">"move38"</span>)) &#123;</span><br><span class="line">                controller.turn(<span class="keyword">false</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code.equals(<span class="string">"move39"</span>)) &#123;</span><br><span class="line">                controller.right(<span class="keyword">false</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code.equals(<span class="string">"move40"</span>)) &#123;</span><br><span class="line">                controller.down(<span class="keyword">false</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code.substring(<span class="number">0</span>, <span class="number">6</span>).equals(<span class="string">"fBlock"</span>)) &#123;</span><br><span class="line">                <span class="keyword">int</span> i = Integer.valueOf(code.substring(<span class="number">6</span>));</span><br><span class="line">                Block block = <span class="keyword">new</span> Block(i);</span><br><span class="line">                setFirstBlock(block);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code.substring(<span class="number">0</span>, <span class="number">6</span>).equals(<span class="string">"nBlock"</span>)) &#123;</span><br><span class="line">                <span class="keyword">int</span> i = Integer.valueOf(code.substring(<span class="number">6</span>));</span><br><span class="line">                Block block = <span class="keyword">new</span> Block(i);</span><br><span class="line">                setNewBlock(block);</span><br><span class="line">            &#125;</span><br><span class="line">            repaint();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line">    <span class="comment">// &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;//GEN-BEGIN:initComponents</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initComponents</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        setBackground(<span class="keyword">new</span> java.awt.Color(<span class="number">204</span>, <span class="number">255</span>, <span class="number">204</span>));</span><br><span class="line">        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));</span><br><span class="line"></span><br><span class="line">        javax.swing.GroupLayout layout = <span class="keyword">new</span> javax.swing.GroupLayout(<span class="keyword">this</span>);</span><br><span class="line">        <span class="keyword">this</span>.setLayout(layout);</span><br><span class="line">        layout.setHorizontalGroup(</span><br><span class="line">            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">            .addGap(<span class="number">0</span>, <span class="number">396</span>, Short.MAX_VALUE)</span><br><span class="line">        );</span><br><span class="line">        layout.setVerticalGroup(</span><br><span class="line">            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">            .addGap(<span class="number">0</span>, <span class="number">296</span>, Short.MAX_VALUE)</span><br><span class="line">        );</span><br><span class="line">    &#125;<span class="comment">// &lt;/editor-fold&gt;//GEN-END:initComponents</span></span><br><span class="line">    <span class="comment">// Variables declaration - do not modify//GEN-BEGIN:variables</span></span><br><span class="line">    <span class="comment">// End of variables declaration//GEN-END:variables</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="MainFrame-java"><a href="#MainFrame-java" class="headerlink" title="MainFrame.java"></a>MainFrame.java</h2><p>这个类是程序的入口，主界面就是由两组<code>GamePanel</code>和<code>CountPanel</code>对象组成的，其中gp和cp是自己的画面，gp2和cp2是对手的画面，自己的画面来自于监听键盘的上下左右操作，对手画面来自于对方传来的上下作用动作进行操作。</p>
<p>并且全局变量大多都是放在这个类中，并提供了get方法进行修改。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mytetris;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Level;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Logger;</span><br><span class="line"><span class="keyword">import</span> socket.GameClient;</span><br><span class="line"><span class="keyword">import</span> socket.GameServer;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainFrame</span> <span class="keyword">extends</span> <span class="title">javax</span>.<span class="title">swing</span>.<span class="title">JFrame</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> Socket socket;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> mod;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">boolean</span> isOver = <span class="keyword">true</span>;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">boolean</span> isPause = <span class="keyword">false</span>;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> point;<span class="comment">//每次加分</span></span><br><span class="line">    <span class="keyword">static</span> GamePanel gp;</span><br><span class="line">    <span class="keyword">static</span> GamePanel gp2;</span><br><span class="line">    <span class="keyword">static</span> CountPanel cp;</span><br><span class="line">    <span class="keyword">static</span> CountPanel cp2;</span><br><span class="line">    GameServer server;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 改变分数显示同时改变速度</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">changeCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cp.setCountText(gp.getGameState().getCount());</span><br><span class="line">        cp2.setCountText(gp2.getGameState().getCount());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">over</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        isOver = <span class="keyword">true</span>;</span><br><span class="line">        isPause = <span class="keyword">false</span>;</span><br><span class="line">        cp.showOver();</span><br><span class="line">        cp2.showOver();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">changeNext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cp.setNextBlock(gp.getNextBlock());</span><br><span class="line">        cp.repaint();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Thread.sleep(<span class="number">100</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException ex) &#123;</span><br><span class="line">            Logger.getLogger(MainFrame<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        cp2.setNextBlock(gp2.getNextBlock());</span><br><span class="line">        cp2.repaint();</span><br><span class="line">        changeCount();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MainFrame</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//规定画板大小</span></span><br><span class="line">        initComponents();</span><br><span class="line">        start.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        pause.setVisible(<span class="keyword">false</span>);</span><br><span class="line"></span><br><span class="line">        cp = <span class="keyword">new</span> CountPanel();</span><br><span class="line">        cp.setSize(<span class="number">100</span>, <span class="number">600</span>);</span><br><span class="line">        cp.setLocation(<span class="number">254</span>, <span class="number">100</span>);</span><br><span class="line">        cp.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(cp);</span><br><span class="line"></span><br><span class="line">        cp2 = <span class="keyword">new</span> CountPanel();</span><br><span class="line">        cp2.setSize(<span class="number">100</span>, <span class="number">600</span>);</span><br><span class="line">        cp2.setLocation(<span class="number">654</span>, <span class="number">100</span>);</span><br><span class="line">        cp2.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(cp2);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.setSize(<span class="number">800</span>, <span class="number">600</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line">    <span class="comment">// &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;//GEN-BEGIN:initComponents</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initComponents</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        go = <span class="keyword">new</span> javax.swing.JButton();</span><br><span class="line">        start = <span class="keyword">new</span> javax.swing.JButton();</span><br><span class="line">        pause = <span class="keyword">new</span> javax.swing.JButton();</span><br><span class="line">        msg = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line">        jLabel1 = <span class="keyword">new</span> javax.swing.JLabel();</span><br><span class="line">        model = <span class="keyword">new</span> javax.swing.JComboBox&lt;&gt;();</span><br><span class="line">        jScrollPane1 = <span class="keyword">new</span> javax.swing.JScrollPane();</span><br><span class="line">        msg2 = <span class="keyword">new</span> javax.swing.JTextArea();</span><br><span class="line">        openHomeButton = <span class="keyword">new</span> javax.swing.JButton();</span><br><span class="line">        joinHomeButton = <span class="keyword">new</span> javax.swing.JButton();</span><br><span class="line">        joinHomeText = <span class="keyword">new</span> javax.swing.JTextField();</span><br><span class="line"></span><br><span class="line">        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);</span><br><span class="line">        setIconImages(<span class="keyword">null</span>);</span><br><span class="line">        addKeyListener(<span class="keyword">new</span> java.awt.event.KeyAdapter() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">keyPressed</span><span class="params">(java.awt.event.KeyEvent evt)</span> </span>&#123;</span><br><span class="line">                formKeyPressed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        go.setBackground(<span class="keyword">new</span> java.awt.Color(<span class="number">255</span>, <span class="number">255</span>, <span class="number">204</span>));</span><br><span class="line">        go.setFont(<span class="keyword">new</span> java.awt.Font(<span class="string">"Lucida Grande"</span>, <span class="number">1</span>, <span class="number">64</span>)); <span class="comment">// NOI18N</span></span><br><span class="line">        go.setForeground(<span class="keyword">new</span> java.awt.Color(<span class="number">102</span>, <span class="number">255</span>, <span class="number">102</span>));</span><br><span class="line">        go.setIcon(<span class="keyword">new</span> javax.swing.ImageIcon(getClass().getResource(<span class="string">"/pictures/巴西.png"</span>))); <span class="comment">// NOI18N</span></span><br><span class="line">        go.setToolTipText(<span class="string">""</span>);</span><br><span class="line">        go.setBorder(<span class="keyword">new</span> javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));</span><br><span class="line">        go.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                goActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        start.setText(<span class="string">"重新开始"</span>);</span><br><span class="line">        start.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                startActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        pause.setText(<span class="string">"暂停"</span>);</span><br><span class="line">        pause.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                pauseActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        msg.setText(<span class="string">"双人游戏哦！"</span>);</span><br><span class="line"></span><br><span class="line">        jLabel1.setText(<span class="string">"模式选择："</span>);</span><br><span class="line"></span><br><span class="line">        model.setModel(<span class="keyword">new</span> javax.swing.DefaultComboBoxModel&lt;&gt;(<span class="keyword">new</span> String[] &#123; <span class="string">"简约模式"</span>, <span class="string">"炫彩模式"</span>, <span class="string">"极速模式"</span> &#125;));</span><br><span class="line">        model.setFocusable(<span class="keyword">false</span>);</span><br><span class="line">        model.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                modelActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        msg2.setColumns(<span class="number">20</span>);</span><br><span class="line">        msg2.setRows(<span class="number">5</span>);</span><br><span class="line">        msg2.setText(<span class="string">"游戏说明：\n键盘←→控制方向，↑旋转俄罗斯方块，↓加速下落\n简约模式，炫彩模式每消除一行加1分；\n极速模式每消除一行加10分；\n道具说明：\n使用道具后将在最低端消除一行，\n每局游戏只有5次使用道具的机会！"</span>);</span><br><span class="line">        msg2.setBorder(<span class="keyword">null</span>);</span><br><span class="line">        msg2.setFocusable(<span class="keyword">false</span>);</span><br><span class="line">        jScrollPane1.setViewportView(msg2);</span><br><span class="line"></span><br><span class="line">        openHomeButton.setText(<span class="string">"创建房间"</span>);</span><br><span class="line">        openHomeButton.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                openHomeButtonActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        joinHomeButton.setText(<span class="string">"加入房间"</span>);</span><br><span class="line">        joinHomeButton.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                joinHomeButtonActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        joinHomeText.addActionListener(<span class="keyword">new</span> java.awt.event.ActionListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;</span><br><span class="line">                joinHomeTextActionPerformed(evt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        javax.swing.GroupLayout layout = <span class="keyword">new</span> javax.swing.GroupLayout(getContentPane());</span><br><span class="line">        getContentPane().setLayout(layout);</span><br><span class="line">        layout.setHorizontalGroup(</span><br><span class="line">            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">            .addGroup(layout.createSequentialGroup()</span><br><span class="line">                .addGap(<span class="number">35</span>, <span class="number">35</span>, <span class="number">35</span>)</span><br><span class="line">                .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">106</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addGap(<span class="number">18</span>, <span class="number">18</span>, <span class="number">18</span>)</span><br><span class="line">                .addComponent(pause)</span><br><span class="line">                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, <span class="number">426</span>, Short.MAX_VALUE)</span><br><span class="line">                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">76</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                    .addComponent(model, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))</span><br><span class="line">                .addGap(<span class="number">32</span>, <span class="number">32</span>, <span class="number">32</span>))</span><br><span class="line">            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()</span><br><span class="line">                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">                    .addGroup(layout.createSequentialGroup()</span><br><span class="line">                        .addGap(<span class="number">50</span>, <span class="number">50</span>, <span class="number">50</span>)</span><br><span class="line">                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">293</span>, javax.swing.GroupLayout.PREFERRED_SIZE))</span><br><span class="line">                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()</span><br><span class="line">                        .addContainerGap()</span><br><span class="line">                        .addComponent(go, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">293</span>, javax.swing.GroupLayout.PREFERRED_SIZE)))</span><br><span class="line">                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)</span><br><span class="line">                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">                    .addGroup(layout.createSequentialGroup()</span><br><span class="line">                        .addComponent(openHomeButton, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">224</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                        .addGap(<span class="number">0</span>, <span class="number">0</span>, Short.MAX_VALUE))</span><br><span class="line">                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()</span><br><span class="line">                        .addGap(<span class="number">0</span>, <span class="number">16</span>, Short.MAX_VALUE)</span><br><span class="line">                        .addComponent(joinHomeButton, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">152</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                        .addGap(<span class="number">18</span>, <span class="number">18</span>, <span class="number">18</span>)</span><br><span class="line">                        .addComponent(joinHomeText, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">153</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                        .addGap(<span class="number">106</span>, <span class="number">106</span>, <span class="number">106</span>))))</span><br><span class="line">            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()</span><br><span class="line">                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)</span><br><span class="line">                .addComponent(msg, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">293</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addGap(<span class="number">199</span>, <span class="number">199</span>, <span class="number">199</span>))</span><br><span class="line">        );</span><br><span class="line">        layout.setVerticalGroup(</span><br><span class="line">            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">            .addGroup(layout.createSequentialGroup()</span><br><span class="line">                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)</span><br><span class="line">                    .addGroup(layout.createSequentialGroup()</span><br><span class="line">                        .addGap(<span class="number">25</span>, <span class="number">25</span>, <span class="number">25</span>)</span><br><span class="line">                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)</span><br><span class="line">                            .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">50</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                            .addComponent(pause, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">50</span>, javax.swing.GroupLayout.PREFERRED_SIZE)))</span><br><span class="line">                    .addGroup(layout.createSequentialGroup()</span><br><span class="line">                        .addGap(<span class="number">12</span>, <span class="number">12</span>, <span class="number">12</span>)</span><br><span class="line">                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">26</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)</span><br><span class="line">                        .addComponent(model, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))</span><br><span class="line">                .addGap(<span class="number">20</span>, <span class="number">20</span>, <span class="number">20</span>)</span><br><span class="line">                .addComponent(msg, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">40</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                .addGap(<span class="number">68</span>, <span class="number">68</span>, <span class="number">68</span>)</span><br><span class="line">                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)</span><br><span class="line">                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()</span><br><span class="line">                        .addComponent(go, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">198</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                        .addGap(<span class="number">18</span>, <span class="number">18</span>, <span class="number">18</span>)</span><br><span class="line">                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">120</span>, javax.swing.GroupLayout.PREFERRED_SIZE))</span><br><span class="line">                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()</span><br><span class="line">                        .addComponent(openHomeButton, javax.swing.GroupLayout.PREFERRED_SIZE, <span class="number">142</span>, javax.swing.GroupLayout.PREFERRED_SIZE)</span><br><span class="line">                        .addGap(<span class="number">44</span>, <span class="number">44</span>, <span class="number">44</span>)</span><br><span class="line">                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, <span class="keyword">false</span>)</span><br><span class="line">                            .addComponent(joinHomeButton, javax.swing.GroupLayout.DEFAULT_SIZE, <span class="number">173</span>, Short.MAX_VALUE)</span><br><span class="line">                            .addComponent(joinHomeText))))</span><br><span class="line">                .addContainerGap(<span class="number">106</span>, Short.MAX_VALUE))</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        pack();</span><br><span class="line">    &#125;<span class="comment">// &lt;/editor-fold&gt;//GEN-END:initComponents</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">formKeyPressed</span><span class="params">(java.awt.event.KeyEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_formKeyPressed</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (evt.getKeyCode() &lt; <span class="number">50</span>) &#123;</span><br><span class="line">                gp.keyPressed(evt);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (NullPointerException e) &#123;</span><br><span class="line">            System.out.println(<span class="string">"还没点开始"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_formKeyPressed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">goActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_goActionPerformed</span></span><br><span class="line">        <span class="comment">// 开始游戏按钮:</span></span><br><span class="line"><span class="comment">//        MainFrame.isOver = false;</span></span><br><span class="line"><span class="comment">//        msg.setVisible(false);</span></span><br><span class="line"><span class="comment">//        msg2.setVisible(false);</span></span><br><span class="line"><span class="comment">//        jScrollPane1.setVisible(false);</span></span><br><span class="line"><span class="comment">//        start.setVisible(true);</span></span><br><span class="line"><span class="comment">//        pause.setVisible(true);</span></span><br><span class="line"><span class="comment">//        gp2 = new GamePanel(false);</span></span><br><span class="line"><span class="comment">//        gp2.setSize(200, 400);</span></span><br><span class="line"><span class="comment">//        gp2.setLocation(450, 100);</span></span><br><span class="line"><span class="comment">//        this.getContentPane().add(gp2);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        server = new GameServer();</span></span><br><span class="line"><span class="comment">//        ClientListen clientListen = new ClientListen(gp2);</span></span><br><span class="line"><span class="comment">//        setSocket(server.getSocket());</span></span><br><span class="line"><span class="comment">//        </span></span><br><span class="line"><span class="comment">//        gp = new GamePanel(true);</span></span><br><span class="line"><span class="comment">//        gp.setSize(200, 400);</span></span><br><span class="line"><span class="comment">//        gp.setLocation(50, 100);</span></span><br><span class="line"><span class="comment">//        this.getContentPane().add(gp);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        changeNext();</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        cp.setVisible(true);</span></span><br><span class="line"><span class="comment">//        cp2.setVisible(true);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        go.setVisible(false);</span></span><br><span class="line"><span class="comment">//        this.requestFocusInWindow();</span></span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_goActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">pauseActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_pauseActionPerformed</span></span><br><span class="line">        <span class="comment">// 暂停按钮:</span></span><br><span class="line">        <span class="keyword">if</span> (isPause) &#123;</span><br><span class="line">            isPause = <span class="keyword">false</span>;</span><br><span class="line">            pause.setText(<span class="string">"暂停"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            isPause = <span class="keyword">true</span>;</span><br><span class="line">            pause.setText(<span class="string">"继续"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.requestFocusInWindow();</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_pauseActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">startActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_startActionPerformed</span></span><br><span class="line">        <span class="comment">// 重新开始按钮:</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            over();</span><br><span class="line">            Thread.sleep(<span class="number">1000</span>);<span class="comment">//等待GamePanel线程死亡</span></span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        MainFrame.isOver = <span class="keyword">false</span>;</span><br><span class="line">        gp = <span class="keyword">new</span> GamePanel(<span class="keyword">true</span>);</span><br><span class="line">        gp.setSize(<span class="number">200</span>, <span class="number">400</span>);</span><br><span class="line">        gp.setLocation(<span class="number">50</span>, <span class="number">100</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(gp);</span><br><span class="line"></span><br><span class="line">        gp2 = <span class="keyword">new</span> GamePanel(<span class="keyword">false</span>);</span><br><span class="line">        gp2.setSize(<span class="number">200</span>, <span class="number">400</span>);</span><br><span class="line">        gp2.setLocation(<span class="number">450</span>, <span class="number">100</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(gp2);</span><br><span class="line"></span><br><span class="line">        changeCount();</span><br><span class="line">        changeNext();</span><br><span class="line">        cp.delOver();</span><br><span class="line">        cp.setProp1Count(<span class="number">5</span>);</span><br><span class="line">        cp2.delOver();</span><br><span class="line">        cp2.setProp1Count(<span class="number">5</span>);</span><br><span class="line">        <span class="keyword">this</span>.requestFocusInWindow();</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_startActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">modelActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_modelActionPerformed</span></span><br><span class="line">        <span class="comment">// 模式切换</span></span><br><span class="line">        String mo = (String) model.getSelectedItem();</span><br><span class="line">        <span class="keyword">switch</span> (mo) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"简约模式"</span>:</span><br><span class="line">                mod = <span class="number">0</span>;</span><br><span class="line">                point = <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"炫彩模式"</span>:</span><br><span class="line">                mod = <span class="number">1</span>;</span><br><span class="line">                point = <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"极速模式"</span>:</span><br><span class="line">                mod = <span class="number">2</span>;</span><br><span class="line">                gp.getGameState().setInterval(<span class="number">100</span>);</span><br><span class="line">                gp2.getGameState().setInterval(<span class="number">100</span>);</span><br><span class="line">                point = <span class="number">10</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_modelActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">openHomeButtonActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_openHomeButtonActionPerformed</span></span><br><span class="line">        <span class="comment">// 开房</span></span><br><span class="line">        MainFrame.isOver = <span class="keyword">false</span>;</span><br><span class="line">        msg.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        msg2.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        jScrollPane1.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        start.setVisible(<span class="keyword">true</span>);</span><br><span class="line">        pause.setVisible(<span class="keyword">true</span>);</span><br><span class="line">        gp2 = <span class="keyword">new</span> GamePanel(<span class="keyword">false</span>);</span><br><span class="line">        gp2.setSize(<span class="number">200</span>, <span class="number">400</span>);</span><br><span class="line">        gp2.setLocation(<span class="number">450</span>, <span class="number">100</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(gp2);</span><br><span class="line"></span><br><span class="line">        server = <span class="keyword">new</span> GameServer(gp2);</span><br><span class="line"><span class="comment">//        ClientListen clientListen = new ClientListen(gp2);</span></span><br><span class="line">        setSocket(server.getSocket());</span><br><span class="line">        </span><br><span class="line">        gp = <span class="keyword">new</span> GamePanel(<span class="keyword">true</span>);</span><br><span class="line">        gp.setSize(<span class="number">200</span>, <span class="number">400</span>);</span><br><span class="line">        gp.setLocation(<span class="number">50</span>, <span class="number">100</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(gp);</span><br><span class="line"></span><br><span class="line">        changeNext();</span><br><span class="line"></span><br><span class="line">        cp.setVisible(<span class="keyword">true</span>);</span><br><span class="line">        cp2.setVisible(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">        go.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        openHomeButton.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        joinHomeButton.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        joinHomeText.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        <span class="keyword">this</span>.requestFocusInWindow();</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_openHomeButtonActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">joinHomeTextActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_joinHomeTextActionPerformed</span></span><br><span class="line">        <span class="comment">// TODO add your handling code here:</span></span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_joinHomeTextActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">joinHomeButtonActionPerformed</span><span class="params">(java.awt.event.ActionEvent evt)</span> </span>&#123;<span class="comment">//GEN-FIRST:event_joinHomeButtonActionPerformed</span></span><br><span class="line">        <span class="comment">// 加入房间</span></span><br><span class="line">        MainFrame.isOver = <span class="keyword">false</span>;</span><br><span class="line">        msg.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        msg2.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        jScrollPane1.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        start.setVisible(<span class="keyword">true</span>);</span><br><span class="line">        pause.setVisible(<span class="keyword">true</span>);</span><br><span class="line">        gp2 = <span class="keyword">new</span> GamePanel(<span class="keyword">false</span>);</span><br><span class="line">        gp2.setSize(<span class="number">200</span>, <span class="number">400</span>);</span><br><span class="line">        gp2.setLocation(<span class="number">450</span>, <span class="number">100</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(gp2);</span><br><span class="line"></span><br><span class="line">        GameClient clientListen = <span class="keyword">new</span> GameClient(gp2, joinHomeText.getText());</span><br><span class="line">        setSocket(clientListen.getSocket());</span><br><span class="line">        </span><br><span class="line">        gp = <span class="keyword">new</span> GamePanel(<span class="keyword">true</span>);</span><br><span class="line">        gp.setSize(<span class="number">200</span>, <span class="number">400</span>);</span><br><span class="line">        gp.setLocation(<span class="number">50</span>, <span class="number">100</span>);</span><br><span class="line">        <span class="keyword">this</span>.getContentPane().add(gp);</span><br><span class="line"></span><br><span class="line">        changeNext();</span><br><span class="line"></span><br><span class="line">        cp.setVisible(<span class="keyword">true</span>);</span><br><span class="line">        cp2.setVisible(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">        go.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        openHomeButton.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        joinHomeButton.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        joinHomeText.setVisible(<span class="keyword">false</span>);</span><br><span class="line">        <span class="keyword">this</span>.requestFocusInWindow();</span><br><span class="line">    &#125;<span class="comment">//GEN-LAST:event_joinHomeButtonActionPerformed</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> args the command line arguments</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        <span class="comment">/* Set the Nimbus look and feel */</span></span><br><span class="line">        <span class="comment">//&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;</span></span><br><span class="line">        <span class="comment">/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.</span></span><br><span class="line"><span class="comment">         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html </span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">for</span> (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="string">"Nimbus"</span>.equals(info.getName())) &#123;</span><br><span class="line">                    javax.swing.UIManager.setLookAndFeel(info.getClassName());</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException ex) &#123;</span><br><span class="line">            java.util.logging.Logger.getLogger(MainFrame<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">java</span>.<span class="title">util</span>.<span class="title">logging</span>.<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InstantiationException ex) &#123;</span><br><span class="line">            java.util.logging.Logger.getLogger(MainFrame<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">java</span>.<span class="title">util</span>.<span class="title">logging</span>.<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IllegalAccessException ex) &#123;</span><br><span class="line">            java.util.logging.Logger.getLogger(MainFrame<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">java</span>.<span class="title">util</span>.<span class="title">logging</span>.<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (javax.swing.UnsupportedLookAndFeelException ex) &#123;</span><br><span class="line">            java.util.logging.Logger.getLogger(MainFrame<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">java</span>.<span class="title">util</span>.<span class="title">logging</span>.<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//&lt;/editor-fold&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Create and display the form */</span></span><br><span class="line">        java.awt.EventQueue.invokeLater(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                MainFrame mf = <span class="keyword">new</span> MainFrame();</span><br><span class="line">                mf.setVisible(<span class="keyword">true</span>);</span><br><span class="line">                mf.requestFocusInWindow();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Variables declaration - do not modify//GEN-BEGIN:variables</span></span><br><span class="line">    <span class="keyword">private</span> javax.swing.JButton go;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JLabel jLabel1;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JScrollPane jScrollPane1;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JButton joinHomeButton;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JTextField joinHomeText;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JComboBox&lt;String&gt; model;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JLabel msg;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JTextArea msg2;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JButton openHomeButton;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JButton pause;</span><br><span class="line">    <span class="keyword">private</span> javax.swing.JButton start;</span><br><span class="line">    <span class="comment">// End of variables declaration//GEN-END:variables</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Socket <span class="title">getSocket</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> socket;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">setSocket</span><span class="params">(Socket s)</span> </span>&#123;</span><br><span class="line">        socket = s;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="socket实现联机"><a href="#socket实现联机" class="headerlink" title="socket实现联机"></a>socket实现联机</h1><p>这里实现联机有很多思路，比如传显示对方的结果的矩阵，或者只传递动作。这里我选择了只传递动作参数，在对战双方的程序中各自进行运算。</p>
<h2 id="SocketUnit-java"><a href="#SocketUnit-java" class="headerlink" title="SocketUnit.java"></a>SocketUnit.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> socket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.OutputStreamWriter;</span><br><span class="line"><span class="keyword">import</span> java.io.PrintWriter;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Level;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Logger;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SocketUtil</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(Socket socket,String string)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (socket != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                PrintWriter pw = <span class="keyword">new</span> PrintWriter(</span><br><span class="line">                        <span class="keyword">new</span> OutputStreamWriter(</span><br><span class="line">                                socket.getOutputStream()));</span><br><span class="line">                pw.println(string);</span><br><span class="line">                pw.flush();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (IOException ex) &#123;</span><br><span class="line">                System.out.println(<span class="string">"error!!"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="GameServer-java"><a href="#GameServer-java" class="headerlink" title="GameServer.java"></a>GameServer.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> socket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.BufferedReader;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStreamReader;</span><br><span class="line"><span class="keyword">import</span> java.io.OutputStreamWriter;</span><br><span class="line"><span class="keyword">import</span> java.io.PrintWriter;</span><br><span class="line"><span class="keyword">import</span> java.net.ServerSocket;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Level;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Logger;</span><br><span class="line"><span class="keyword">import</span> mytetris.GamePanel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GameServer</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    Socket socket;</span><br><span class="line">    GamePanel gamePanel;</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">GameServer</span><span class="params">(GamePanel gamePanel)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">this</span>.gamePanel = gamePanel;</span><br><span class="line">            System.out.println(<span class="string">"服务器开启！"</span>);</span><br><span class="line">            ServerSocket serverSocket = <span class="keyword">new</span> ServerSocket(<span class="number">50000</span>);</span><br><span class="line">            socket = serverSocket.accept();</span><br><span class="line">            System.out.println(<span class="string">"----------------------------"</span>);</span><br><span class="line">            System.out.println(<span class="string">"有新的客户端连接到服务器喽！"</span>);</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">this</span>).start();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException ex) &#123;</span><br><span class="line">            Logger.getLogger(GameServer<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"我开始收听客户端！"</span>);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            BufferedReader br = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(</span><br><span class="line">                            socket.getInputStream()));</span><br><span class="line">            String code;</span><br><span class="line">            <span class="keyword">while</span> (!(code = br.readLine()).equals(<span class="string">"bye"</span>)) &#123;</span><br><span class="line">                gamePanel.getPressed(code);</span><br><span class="line">                gamePanel.repaint();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">"客户端已经关闭了对话！"</span>);</span><br><span class="line">            br.close();</span><br><span class="line">            socket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(<span class="string">"服务器已经关闭"</span>);</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Socket <span class="title">getSocket</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> socket;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="GameClient-java"><a href="#GameClient-java" class="headerlink" title="GameClient.java"></a>GameClient.java</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> socket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.BufferedReader;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStreamReader;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Level;</span><br><span class="line"><span class="keyword">import</span> java.util.logging.Logger;</span><br><span class="line"><span class="keyword">import</span> mytetris.GamePanel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GameClient</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    Socket clientSocket;</span><br><span class="line">    GamePanel gamePanel;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">GameClient</span><span class="params">(GamePanel gamePanel, String ip)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">this</span>.gamePanel = gamePanel;</span><br><span class="line">            <span class="keyword">this</span>.clientSocket = <span class="keyword">new</span> Socket(ip, <span class="number">50000</span>);</span><br><span class="line">            <span class="keyword">new</span> Thread(<span class="keyword">this</span>).start();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException ex) &#123;</span><br><span class="line">            Logger.getLogger(GameClient<span class="class">.<span class="keyword">class</span>.<span class="title">getName</span>()).<span class="title">log</span>(<span class="title">Level</span>.<span class="title">SEVERE</span>, <span class="title">null</span>, <span class="title">ex</span>)</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            BufferedReader br = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(</span><br><span class="line">                            clientSocket.getInputStream()));</span><br><span class="line">            String code;</span><br><span class="line">            <span class="keyword">while</span> (!(code = br.readLine()).equals(<span class="string">"bye"</span>)) &#123;</span><br><span class="line">                gamePanel.getPressed(code);</span><br><span class="line">                gamePanel.repaint();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">"服务端已经关闭了对话！"</span>);</span><br><span class="line">            br.close();</span><br><span class="line">            clientSocket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(<span class="string">"客户端已经关闭"</span>);</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> Socket <span class="title">getSocket</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> clientSocket;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
      

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Zlatanlong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://lclong.top/2018/11/17/Java/java%E5%AE%9E%E7%8E%B0%E4%BF%84%E7%BD%97%E6%96%AF%E8%81%94%E6%9C%BA%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97/" title="Java实现俄罗斯联机俄罗斯方块">https://lclong.top/2018/11/17/Java/java实现俄罗斯联机俄罗斯方块/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/socket/" rel="tag"><i class="fa fa-tag"></i> socket</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/10/07/(%E8%BD%AC)java%20List%20%E6%8E%92%E5%BA%8F%20Collections.sort()/" rel="prev" title="(转)Java List 排序 Collections.sort()">
      <i class="fa fa-chevron-left"></i> (转)Java List 排序 Collections.sort()
    </a></div>
      <div class="post-nav-item">
    <a href="/2018/12/30/hexo/hexo%E8%B8%A9%E5%9D%91%E8%AE%B0%E5%BD%95/" rel="next" title="Hexo踩坑记录(持续更新)">
      Hexo踩坑记录(持续更新) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  
  </div>


          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#项目结构"><span class="nav-number">2.</span> <span class="nav-text">项目结构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#俄罗斯方块主体代码"><span class="nav-number">3.</span> <span class="nav-text">俄罗斯方块主体代码</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Controller-java"><span class="nav-number">3.1.</span> <span class="nav-text">Controller.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Block-java"><span class="nav-number">3.2.</span> <span class="nav-text">Block.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GameState-java"><span class="nav-number">3.3.</span> <span class="nav-text">GameState.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CountPanel-java"><span class="nav-number">3.4.</span> <span class="nav-text">CountPanel.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GamePanel-java"><span class="nav-number">3.5.</span> <span class="nav-text">GamePanel.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MainFrame-java"><span class="nav-number">3.6.</span> <span class="nav-text">MainFrame.java</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#socket实现联机"><span class="nav-number">4.</span> <span class="nav-text">socket实现联机</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#SocketUnit-java"><span class="nav-number">4.1.</span> <span class="nav-text">SocketUnit.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GameServer-java"><span class="nav-number">4.2.</span> <span class="nav-text">GameServer.java</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GameClient-java"><span class="nav-number">4.3.</span> <span class="nav-text">GameClient.java</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Zlatanlong"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Zlatanlong</p>
  <div class="site-description" itemprop="description">生活就像巧克力🍫</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3psYXRhbmxvbmc=" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zlatanlong"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxvbmctdHhnY0Bmb3htYWlsLmNvbQ==" title="E-Mail → mailto:long-txgc@foxmail.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      关注列表
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9sZWZsYWNvbi5naXRodWIuaW8v" title="https:&#x2F;&#x2F;leflacon.github.io&#x2F;">leflacon</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9ib2Jib3NzLmdpdGh1Yi5pby8=" title="https:&#x2F;&#x2F;bobboss.github.io&#x2F;">BobBoss</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cDovL29wdGltaXN0aWNhdC54eXovdXNlcj91c2VySWQ9bHpkY2w=" title="http:&#x2F;&#x2F;optimisticat.xyz&#x2F;user?userId&#x3D;lzdcl">lzdcl</span>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zlatanlong</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">166k</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> 强力驱动 v4.2.1
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> v7.7.1
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script color='0,0,255' opacity='0.5' zIndex='-1' count='99' src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@latest/canvas-nest-nomobile.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'H5inyassVpcpItLTuMJH9aBg-gzGzoHsz',
      appKey     : 'mBzB6QB3deD6zw8YEIKVQdzz',
      placeholder: "快来说两句吧。。。",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
